package java67_adv9;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

//聊天的线程
public class ObjectThread extends Thread {
	private Socket s;
	private Map<String, Object> words = new HashMap<>();

	// ArrayList
	public ObjectThread(Socket s) {
		this.s = s;
		super.setName(String.valueOf(s.getInetAddress()));
		words.put("one", 1);
		words.put("two", 2);
		words.put("sb", 250);
		words.put("雷锋", new KindMan("leifeng", 32));
		words.put("吕洞宾", new KindMan("吕洞宾", 3000));
	}

	@Override
	public void run() {
		// 先从客户端那里获得要下载的文件名,发送给客户端
		System.out.println(s.getInetAddress() + ":" + s.getPort() + "连接上了...");
		try {
			// InputStream in = s.getInputStream();
			OutputStream out = s.getOutputStream();
			ObjectOutputStream oout = new ObjectOutputStream(out);
			oout.writeObject(words);
			// >>oout.flush();
			oout.reset();
			System.out.println("对象文件发送给" + s.getInetAddress() + "成功");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (s != null)
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}
